package control

import (
	"bms/model"
	"bms/service"
	"html/template"
	"log"
	"net/http"
	"strconv"
)

func GetBook(w http.ResponseWriter, r *http.Request) {
	books := service.GetBook()
	/*//测试
	bytes, err := json.Marshal(books)
	if err != nil {
		return
	}
	w.Write(bytes)*/
	t := template.Must(template.ParseFiles("view/pages/book/books_list.html"))
	t.Execute(w, books)
}

func GetBookByPage(w http.ResponseWriter, r *http.Request) {
	currentPage, _ := strconv.Atoi(r.FormValue("currentPage"))
	if currentPage <= 0 {
		currentPage = 1
	}
	page := service.GetBookByPage(int64(currentPage))
	if page != nil {
		flag, session := IsLogin(r)
		if flag {
			//已经登录啦
			page.IsLogin = true
			page.Username = session.Username
		}
		t := template.Must(template.ParseFiles("view/pages/book/books_manage.html"))
		t.Execute(w, page)
	} else {
		w.Write([]byte("没有这一页"))
	}

}

func GetBookByTitleOrAuthor(w http.ResponseWriter, r *http.Request) {
	currentPage, _ := strconv.Atoi(r.FormValue("currentPage"))
	if currentPage <= 0 {
		currentPage = 1
	}
	title := r.FormValue("title")
	author := r.FormValue("author")
	var page *model.Page
	if title == "" && author == "" {
		page = service.GetBookByPage(int64(currentPage))
	} else {
		page = service.GetBookByTitleOrAuthor(int64(currentPage), title, author)
		page.Title = title
		page.Author = author
	}
	if page != nil {
		flag, session := IsLogin(r)
		if flag {
			//已经登录啦
			page.IsLogin = true
			page.Username = session.Username
		}
		t := template.Must(template.ParseFiles("view/pages/book/books_list.html"))
		t.Execute(w, page)
	} else {
		w.Write([]byte("没有这一页"))
	}

}

func QueryBookById(w http.ResponseWriter, r *http.Request) {
	bookId, _ := strconv.Atoi(r.FormValue("bookId"))
	book := service.QueryBookById(int64(bookId))
	if book.Id > 0 {
		//修改
		t := template.Must(template.ParseFiles("view/pages/book/books_edit.html"))
		t.Execute(w, book)
	} else {
		//添加
		t := template.Must(template.ParseFiles("view/pages/book/books_edit.html"))
		t.Execute(w, "")
	}
}

func EditBook(w http.ResponseWriter, r *http.Request) {
	bookId, _ := strconv.Atoi(r.FormValue("bookId"))
	title := r.PostFormValue("title")
	author := r.PostFormValue("author")
	publish := r.PostFormValue("publish")
	price, _ := strconv.ParseFloat(r.PostFormValue("price"), 64)
	borrow, _ := strconv.Atoi(r.PostFormValue("borrow"))
	remain, _ := strconv.Atoi(r.PostFormValue("remain"))
	book := &model.Book{
		Id:      int64(bookId),
		Title:   title,
		Author:  author,
		Publish: publish,
		Price:   price,
		Borrow:  int64(borrow),
		Remain:  int64(remain),
		ImgPath: "static/img/default.jpg",
	}
	log.Println("----bookId----", book.Id)
	if book.Id > 0 {
		//图书修改
		err := service.ReviseBook(book)
		if err != nil {
			return
		}
	} else {
		//新书上架
		err := service.AddNewBook(book)
		if err != nil {
			return
		}
	}
	GetBookByPage(w, r)
}

func DeleteBookById(w http.ResponseWriter, r *http.Request) {
	bookId, _ := strconv.Atoi(r.FormValue("bookId"))
	//判断这本书是否存在
	book := service.QueryBookById(int64(bookId))
	if book != nil {
		log.Println(bookId, "bookId------------")
		log.Println(book.IsBorrow, "book.IsBorrow,-----------")
		//删除之前需要判断这本书是否已经被借，如果被借 不允许删
		if book.IsBorrow == 2 {
			w.Write([]byte("这本书已经被借，无法删除"))
		} else {
			err := service.DeleteBookById(int64(bookId))
			if err != nil {
				return
			}
			GetBookByPage(w, r)
		}
	} else {
		w.Write([]byte("这本书不存在"))
	}
}
